﻿using Occt;
using System.Collections.Generic;

namespace Tx.OCC
{
    public static class OccAisInteractiveContextEx
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="aisContext"></param>
        /// <returns></returns>
        public static TopoDS_Shape GetDetectedShape(this AIS_InteractiveContext aisContext)
        {
            var owner = aisContext.DetectedOwner;
            if (owner == null || owner.IsNull)
                return null;
            var brepOwner = owner as StdSelect_BRepOwner;
            if (brepOwner == null || brepOwner.IsNull)
                return null;
            return brepOwner.Shape;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="aisContext"></param>
        /// <param name="pointList"></param>
        /// <param name="bAllowOverlapDetection"></param>
        /// <param name="theView"></param>
        /// <returns></returns>
        public static SelectMgr_ViewerSelector Pick(this AIS_InteractiveContext aisContext, IList<gp_Pnt2d> pointList,
            bool bAllowOverlapDetection, V3d_View theView)
        {
            var selector = aisContext.MainSelector;
            selector.AllowOverlapDetection(bAllowOverlapDetection);
            selector.Pick(new TColgp_Array1OfPnt2d(pointList), theView);
            return selector;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="aisContext"></param>
        /// <param name="theXPMin"></param>
        /// <param name="theYPMin"></param>
        /// <param name="theXPMax"></param>
        /// <param name="theYPMax"></param>
        /// <param name="bAllowOverlapDetection"></param>
        /// <param name="theView"></param>
        /// <returns></returns>
        public static SelectMgr_ViewerSelector Pick(this AIS_InteractiveContext aisContext, int theXPMin, int theYPMin,
            int theXPMax, int theYPMax, bool bAllowOverlapDetection, V3d_View theView)
        {
            var selector = aisContext.MainSelector;
            selector.AllowOverlapDetection(bAllowOverlapDetection);
            selector.Pick(theXPMin, theYPMin, theXPMax, theYPMax, theView);
            return selector;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="selector"></param>
        /// <returns></returns>
        public static IEnumerable<AIS_InteractiveObject> GetPickedInteractiveObjects(
            this SelectMgr_ViewerSelector selector)
        {
            for (int aPickIter = 1; aPickIter < selector.NbPicked; aPickIter++)
            {
                var owner = selector.Picked(aPickIter);
                if (owner == null || owner.IsNull || !owner.HasSelectable)
                    continue;

                var interactive = owner.Selectable as AIS_InteractiveObject;
                if (interactive != null && !interactive.IsNull)
                    yield return interactive;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="selector"></param>
        /// <returns></returns>
        public static IEnumerable<TopoDS_Shape> GetPickedTopoDSShapes(this SelectMgr_ViewerSelector selector)
        {
            for (int aPickIter = 1; aPickIter < selector.NbPicked; aPickIter++)
            {
                var owner = selector.Picked(aPickIter);
                if (owner == null || owner.IsNull || !owner.HasSelectable)
                    continue;

                var brepOwner = owner as StdSelect_BRepOwner;
                if (brepOwner != null && !brepOwner.IsNull && brepOwner.HasShape)
                {
                    yield return brepOwner.Shape;
                }
            }
        }
    }
}
